package com.szy.redstonemcu;

import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.WorldClient;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.fml.relauncher.Side;

public class MessageHandlerOnClient implements IMessageHandler<McuMessage, IMessage> {
    @Override
    public IMessage onMessage(McuMessage message, MessageContext ctx) {
        if (ctx.side != Side.CLIENT) {
            System.err.println("ClientToServer received on wrong side:" + ctx.side);
            return null;
        }
        Minecraft minecraft = Minecraft.getMinecraft();
        final WorldClient worldClient = minecraft.world;
        minecraft.addScheduledTask(new Runnable() {
            @Override
            public void run() {
                processMessage(worldClient,message);
            }
        });

        return null;
    }
    public static EditScreen editScreen;
    void processMessage(WorldClient worldClient, McuMessage message){
        //RedstoneMCU.logger.info("received message from server");
        RedstoneMCU.logger.info("client receive message, type="+message.typeNum);
        TileEntity entity=worldClient.getTileEntity(message.pos);
        if(message.typeNum == McuMessage.TYPE_READ_CODE){
            if(entity instanceof McuBaseEntity){
                editScreen=new EditScreen(message.code,message.pos);
                Minecraft.getMinecraft().displayGuiScreen(editScreen);
            }

        } else if (message.typeNum == McuMessage.TYPE_RETURN_ASSEMBLE_RESULT) {
            if(entity instanceof McuBaseEntity){
                editScreen.showResult(message.code);
            }
        }

    }
}
